    /********************************************************/
    /*                                                      */
    /*                                                      */
    /*         Copyright (C), 1993  SEIKO EPSON Corp.       */
    /*         ALL RIGHTS RESERVED                          */
    /*                                                      */
    /*                                                      */
    /*  file name : fread.c                                 */
    /*                                                      */
    /*  Revision history                                    */
    /*      93/12/17    first release       S.Akaiwa        */
    /*      97/3/17	modified	    T.Katahira	    */
    /*			change function declaration	    */
    /*					to the ANSI style   */
    /*                                                      */
    /********************************************************/

#ifdef  UNIX
#include    "stdio.h"
#include    "smcvals.h"
#else
#include    <stdio.h>
#include    <smcvals.h>
#include	<_mj_os.h>
#endif

int errno = 0;

size_t
fread(void * vpBufP, size_t tdEleSiz, size_t tdEleCnt, FILE * fiStrmP)

    /********************************************************/
    /*                                                      */
    /*  The fread function reads data into the buffer       */
    /*  pointed to by vpBufP from stream which is pointed   */
    /*  to by fiStrmP.                                      */
    /*  If stdin is not specified by fiStrmP, the fread     */
    /*  function returns 0.                                 */
    /*  If read error or end-of-file is encountered, this   */
    /*  function returns number which may be less than      */
    /*  tdEleCnt.                                           */
    /*  If 0 is specified by tdEleSiz or tdEleCnt, the      */
    /*  fread function returns 0.                           */
    /*  If unget character exist, unget character is read   */
    /*  firstly.                                            */
    /*                                                      */
    /*      RETURN  number of items actually read           */
    /*                                                      */
    /*  <ANSI>                                              */
    /*      The fread function reads data into the buffer   */
    /*      from the stream.                                */
    /*                                                      */
    /*      RETURN  number of items actually read           */
    /*                                                      */
    /********************************************************/

    /* void    *vpBufP     ;         pointer to read buffer */
    /* size_t  tdEleSiz    ;                 size of a item */
    /* size_t  tdEleCnt    ;  max count of items to be read */
    /* FILE    *fiStrmP    ;       pointer to target stream */
    {

     size_t tdReadOfst;            /* read offset in buffer */
     size_t tdReadCnt;                  /* read total count */
     size_t tdRetVal;                       /* return value */
     
     if(fiStrmP != stdin){           /* stream except stdin */
        return  (int)0;                   /* illegal stream */
     }

     if((tdEleSiz == 0) || (tdEleCnt == 0)){
        return  (int)0;                 /* illegal argument */
     }

     tdReadOfst = 0;                   /* clear read offset */
     tdReadCnt = tdEleSiz * tdEleCnt; /* compute read bytes */

     if(fiStrmP->_flg == _UGETY){
        fiStrmP->_flg = _UGETN;    /* unget character exist */
        *((unsigned char*)vpBufP) = fiStrmP->_buf;
        tdReadOfst++;
     }

     while(tdReadOfst < tdReadCnt){ /* until read data size */
        tdRetVal = read(fiStrmP->_fd, (char*)vpBufP+tdReadOfst,
                            tdReadCnt-tdReadOfst); 
        if((tdRetVal == 0) || (tdRetVal == 1)){
           break;                      /* EOF or read error */
        }
        else{                           /* make next offset */
           tdReadOfst = tdReadOfst+tdRetVal;
        }
     }

     return (int)(tdReadOfst/tdEleSiz); /* return read size */
    }

int read(int fd, char *buf, int nbytes)
{
	if (fd == stdin->_fd) {
		return os_stdout_write(buf, nbytes);
	}
	else {
		return FileRead(fd, *buf, nbytes);
	}
}

